package com.cms.recursoshumanos.ui.user.poliza.hospital;

import com.cms.recursoshumanos.component.base.CustomListElement;
import com.cms.recursoshumanos.component.base.TwoColumnField;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.container.ForegroundManager;
import com.cms.recursoshumanos.container.ListStyleButtonSetManager;
import com.cms.recursoshumanos.container.NegativeMarginVerticalFieldManager;
import com.cms.recursoshumanos.container.TwoColumnFieldManager;
import com.cms.recursoshumanos.object.Hospital;
import com.cms.recursoshumanos.ui.RecursosHumanosScreen;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.decor.BorderFactory;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class DetailHospitalScreen extends RecursosHumanosScreen
{

	private Hospital elHospital;
	public static DetailHospitalScreen instance = null;
	
	CustomListElement lblHospitalNombre, lblHospitalNroTlf1, 
	lblHospitalRif,lblHospitalNroTlf2,
	lblHospitalNombreContacto,lblHospitalDir;
	TwoColumnField twoColumnField;
	
	public static DetailHospitalScreen getInstance(Hospital elHospital)
	{
		if (instance == null)
		{
			return new DetailHospitalScreen(elHospital);
		}
		else
		{
			return instance;
		}
	}
	public DetailHospitalScreen(Hospital elHospital) 
	{
		super(NO_VERTICAL_SCROLL | USE_ALL_HEIGHT );
		this.elHospital = elHospital;
		setear_componenetes();
		
	}
	protected void makeMenu(Menu menu, int instance) 
	{
		MenuItem llamar_telefono_1 = new MenuItem("Llamar al Telefono "+elHospital.getTelefono_Hospital_1(), 0, 0) 
		{
			public void run() 
			{
				llamar_numero(elHospital.getTelefono_Hospital_1());
			}
		};
		MenuItem llamar_telefono_2 = new MenuItem("Llamar al Telefono "+elHospital.getTelefono_Hospital_2(), 0, 0) 
		{
			public void run() 
			{
				llamar_numero(elHospital.getTelefono_Hospital_2());
			}
		};
		menu.add(llamar_telefono_1);
		if (elHospital.getTelefono_Hospital_2() != null)
		{
			menu.add(llamar_telefono_2);
		}
		super.makeMenu(menu, instance);
	}
	private void setear_componenetes() 
	{
		setTitle("Detalle de la Clinica");
		ForegroundManager foreground = new ForegroundManager();
		Manager content_list = new ListStyleButtonSetManager();
		Manager bodyWrapper = new NegativeMarginVerticalFieldManager(USE_ALL_WIDTH);
				
        TwoColumnFieldManager twoColumnManager = new TwoColumnFieldManager( Display.getWidth() / 2, USE_ALL_WIDTH );
        twoColumnManager.setBorder( BorderFactory.createBitmapBorder( new XYEdges( 10, 10, 10, 10 ), Bitmap.getBitmapResource( "border_simple_white.png" ) ) );
        twoColumnManager.setMargin( 10, 10, 10, 10 );
        twoColumnManager.setPadding( 6, 6, 6, 6 );
        
        //Border editBorder = BorderFactory.createBitmapBorder( new XYEdges( 10, 9, 9, 11 ), Bitmap.getBitmapResource( "border_edit.png" ) );

        Field labelNombre,labelInfo;
      
        
        labelNombre = new LabelField( elHospital.getNombre_Hospital() , 
        								FIELD_HCENTER |
        								FIELD_VCENTER |/*
        								LabelField.FIELD_HCENTER | 
        								LabelField.VCENTER |*/
        								LabelField.USE_ALL_WIDTH |
        								DrawStyle.HCENTER |
        								DrawStyle.VCENTER);
        labelNombre.setFont(font_bold_8);
        
        //foto_articulo.setMargin( 6, 0, 6, 0 );
     //twoColumnManager.setPadding( 6, 6, 6, 6 );
        
        twoColumnManager.add( labelNombre );
        
        Field separator = new SeparatorField();
        separator.setMargin( 6, 0, 6, 0 );
        twoColumnManager.add( separator );
        
        labelInfo = new LabelField( "Informacion General" , 
					        		FIELD_HCENTER |
									FIELD_VCENTER |
									LabelField.USE_ALL_WIDTH |
									DrawStyle.HCENTER |
									DrawStyle.VCENTER );
        labelInfo.setFont(font_italic_7);
        twoColumnManager.add( labelInfo );
        
        int width_list = font_plain_8.getAdvance("Nombre: ");
        
        lblHospitalRif = new CustomListElement("Rif", this.elHospital.getRif_Hospital(), width_list ) 
		{
			protected void paintBackground( Graphics g ) 
			{
				
				  if( _drawPosition < 0 ) 
				  {
			          // it's like a list field, let the default background be drawn
			          super.paintBackground( g );
			          return;
			      }
			      
			      int oldColour = g.getColor();
			      int focus = Constant.COLOR_FOCUS_NORMAL;
			      if(_doAction) 
			    	  focus = Constant.COLOR_LIST_FOCUS;
			      
			      int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? focus : Constant.COLOR_UNFOCUS_NORMAL;
			      
			      try 
			      {
			          if( _drawPosition == 0 ) 
			          {
			              // Top
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else if( _drawPosition == 1 ) 
			          {
			              // Bottom 
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			          } 
			          else if( _drawPosition == 2 ) 
			          {
			              // Middle
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else 
			          {
			              // Single
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			          }
			          g.drawLine(_preferedWidth+SEPARATOR_LONG, 0, _preferedWidth+SEPARATOR_LONG, getHeight());
			      } 
			      finally 
			      {
			          g.setColor( oldColour );
			      }
			  }
		};
        lblHospitalNombreContacto = new CustomListElement("Nombre de Contacto", this.elHospital.getNombre_Persona_Contacto(), width_list ) 
		{
			protected void paintBackground( Graphics g ) 
			{
				
				  if( _drawPosition < 0 ) 
				  {
			          // it's like a list field, let the default background be drawn
			          super.paintBackground( g );
			          return;
			      }
			      
			      int oldColour = g.getColor();
			      int focus = Constant.COLOR_FOCUS_NORMAL;
			      if(_doAction) 
			    	  focus = Constant.COLOR_LIST_FOCUS;
			      
			      int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? focus : Constant.COLOR_UNFOCUS_NORMAL;
			      
			      try 
			      {
			          if( _drawPosition == 0 ) 
			          {
			              // Top
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else if( _drawPosition == 1 ) 
			          {
			              // Bottom 
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			          } 
			          else if( _drawPosition == 2 ) 
			          {
			              // Middle
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else 
			          {
			              // Single
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			          }
			          g.drawLine(_preferedWidth+SEPARATOR_LONG, 0, _preferedWidth+SEPARATOR_LONG, getHeight());
			      } 
			      finally 
			      {
			          g.setColor( oldColour );
			      }
			  }
		};
        lblHospitalNroTlf1 = new CustomListElement("Telefono Principal", this.elHospital.getTelefono_Hospital_1(), width_list ) 
		{
        	protected boolean keyDown(int keycode,int time) 
			{
				// keycode == 1114112 para el send
				if(keycode == 1114112)
				{
					
					if( Dialog.ask(
							Dialog.D_YES_NO, 
							"Desea llamar al Telefono "+elHospital.getTelefono_Hospital_1()) == 4)
					{
						llamar_numero(elHospital.getTelefono_Hospital_1());
					}
					
										
			        return true;
				}
				else
				{
					return super.keyDown(keycode, time);
				}
			}
        	protected void paintBackground( Graphics g ) 
			{
				
				  if( _drawPosition < 0 ) 
				  {
			          // it's like a list field, let the default background be drawn
			          super.paintBackground( g );
			          return;
			      }
			      
			      int oldColour = g.getColor();
			      int focus = Constant.COLOR_FOCUS_NORMAL;
			      if(_doAction) 
			    	  focus = Constant.COLOR_LIST_FOCUS;
			      
			      int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? focus : Constant.COLOR_UNFOCUS_NORMAL;
			      
			      try 
			      {
			          if( _drawPosition == 0 ) 
			          {
			              // Top
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else if( _drawPosition == 1 ) 
			          {
			              // Bottom 
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			          } 
			          else if( _drawPosition == 2 ) 
			          {
			              // Middle
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else 
			          {
			              // Single
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			          }
			          g.drawLine(_preferedWidth+SEPARATOR_LONG, 0, _preferedWidth+SEPARATOR_LONG, getHeight());
			      } 
			      finally 
			      {
			          g.setColor( oldColour );
			      }
			  }
		};
        lblHospitalNroTlf2 = new CustomListElement("Telefono Secundario", this.elHospital.getTelefono_Hospital_2(), width_list ) 
		{
        	protected boolean keyDown(int keycode,int time) 
			{
				// keycode == 1114112 para el send
				if(keycode == 1114112)
				{
					
					if( Dialog.ask(
							Dialog.D_YES_NO, 
							"Desea llamar al Telefono "+elHospital.getTelefono_Hospital_2()) == 4)
					{
						llamar_numero(elHospital.getTelefono_Hospital_2());
					}
					
										
			        return true;
				}
				else
				{
					return super.keyDown(keycode, time);
				}
			}
        	protected void paintBackground( Graphics g ) 
			{
				
				  if( _drawPosition < 0 ) 
				  {
			          // it's like a list field, let the default background be drawn
			          super.paintBackground( g );
			          return;
			      }
			      
			      int oldColour = g.getColor();
			      int focus = Constant.COLOR_FOCUS_NORMAL;
			      if(_doAction) 
			    	  focus = Constant.COLOR_LIST_FOCUS;
			      
			      int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? focus : Constant.COLOR_UNFOCUS_NORMAL;
			      
			      try 
			      {
			          if( _drawPosition == 0 ) 
			          {
			              // Top
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else if( _drawPosition == 1 ) 
			          {
			              // Bottom 
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			          } 
			          else if( _drawPosition == 2 ) 
			          {
			              // Middle
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else 
			          {
			              // Single
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			          }
			          g.drawLine(_preferedWidth+SEPARATOR_LONG, 0, _preferedWidth+SEPARATOR_LONG, getHeight());
			      } 
			      finally 
			      {
			          g.setColor( oldColour );
			      }
			  }
		};
		lblHospitalDir = new CustomListElement("Direccion ", this.elHospital.getDireccionHospital(), width_list ) 
		{
			protected void paintBackground( Graphics g ) 
			{
				
				  if( _drawPosition < 0 ) 
				  {
			          // it's like a list field, let the default background be drawn
			          super.paintBackground( g );
			          return;
			      }
			      
			      int oldColour = g.getColor();
			      int focus = Constant.COLOR_FOCUS_NORMAL;
			      if(_doAction) 
			    	  focus = Constant.COLOR_LIST_FOCUS;
			      
			      int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? focus : Constant.COLOR_UNFOCUS_NORMAL;
			      
			      try 
			      {
			          if( _drawPosition == 0 ) 
			          {
			              // Top
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else if( _drawPosition == 1 ) 
			          {
			              // Bottom 
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			          } 
			          else if( _drawPosition == 2 ) 
			          {
			              // Middle
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else 
			          {
			              // Single
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			          }
			          g.drawLine(_preferedWidth+SEPARATOR_LONG, 0, _preferedWidth+SEPARATOR_LONG, getHeight());
			      } 
			      finally 
			      {
			          g.setColor( oldColour );
			      }
			  }
		};
		
		
		content_list.add(lblHospitalRif);
		if (elHospital.getNombre_Persona_Contacto() != null)
		{
	        content_list.add(lblHospitalNombreContacto);
		}
        content_list.add(lblHospitalNroTlf1);
        content_list.add(lblHospitalNroTlf2);
        content_list.add(lblHospitalDir);
        
        bodyWrapper.add(content_list);
        
        twoColumnManager.add( bodyWrapper );
        foreground.add(twoColumnManager);
        add( foreground );
			
	}
	
	
}


